//
//  DDBaseViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/11.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDBaseViewController.h"
#import "MBProgressHUD.h"
#import "UIImage+Extension.h"

@interface DDBaseViewController ()
@property (strong, nonatomic) UIView *noContentView;
@property (strong, nonatomic) MBProgressHUD *hud;
@end

@implementation DDBaseViewController
- (UIView *)navBarView {
    if (!_navBarView) {
        UIView *navBarView = [[UIView alloc] init];
        navBarView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,64);
        [self.view addSubview:navBarView];
        _navBarView = navBarView;
    }
    return _navBarView;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setLeftBarItem];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navBarView.backgroundColor = [UIColor whiteColor];
}
- (void)setLeftBarItem
{
    UIImage *image = [LoadImage(@"back")imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(gotoBack)];
    self.navigationItem.leftBarButtonItem = item;
}
- (void)gotoBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showHUD
{
    if (!_hud) {
        self.hud =[[MBProgressHUD alloc] initWithView:self.view];
        _hud.mode = MBProgressHUDModeIndeterminate;
        _hud.removeFromSuperViewOnHide = YES;
    }
    [self.view addSubview:_hud];
    [_hud showAnimated:YES];
}
- (void)showMessageHUD:(NSString *)msg delayHide:(NSTimeInterval)delay
{
    if (!_hud) {
        self.hud = [[MBProgressHUD alloc] initWithView:self.view];
        _hud.removeFromSuperViewOnHide = YES;
    }
    [self.view addSubview:_hud];
    _hud.mode = MBProgressHUDModeText;
    _hud.detailsLabel.text = msg;
    _hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [_hud showAnimated:YES];
    [_hud hideAnimated:YES afterDelay:delay];
}
- (void)showMessageHUD:(NSString *)msg
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.label.text = msg;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [self.view addSubview:hud];
    [hud showAnimated:YES];
}
- (void)hideHUD
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    self.hud = nil;
}
- (void)showNoContentViewWithOriginY:(CGFloat)y image:(NSString *)pic message:(NSString *)msg
{
    if (!_noContentView) {
        self.noContentView = [[UIView alloc]initWithFrame:CGRectMake(0, y, ScreenWidth, ScreenHeight-y)];
        [self.view addSubview:_noContentView];
        UIImage *icon = LoadImage(pic);
        UIImageView *imgView = [[UIImageView alloc]initWithImage:icon];
        imgView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
        [_noContentView addSubview:imgView];
        imgView.center = CGPointMake(ScreenWidth/2.0, (ScreenHeight-y-icon.size.height)/2.0);
        
        KKLabel *contentLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(msg,RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentCenter).laFrame(CGRectMake(0,imgView.frame.origin.y+imgView.frame.size.height+5, ScreenWidth, 30));
        }];
        [_noContentView addSubview:contentLbl];
    }
}
- (void)showNoContentViewIn:(UIView *)bgView image:(NSString *)pic withOriginY:(CGFloat)y message:(NSString *)msg
{
    if (!_noContentView) {
        self.noContentView = [[UIView alloc]initWithFrame:CGRectMake(0, y, ScreenWidth, bgView.bounds.size.height-y)];
        [bgView addSubview:_noContentView];
        UIImage *icon = LoadImage(pic);
        UIImageView *imgView = [[UIImageView alloc]initWithImage:icon];
        imgView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
        [_noContentView addSubview:imgView];
        imgView.center = CGPointMake(ScreenWidth/2.0, (bgView.bounds.size.height-y-icon.size.height)/2.0);
        
        KKLabel *contentLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(msg,RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentCenter).laFrame(CGRectMake(0,imgView.frame.origin.y+imgView.frame.size.height+5, ScreenWidth, 30));
        }];
        [_noContentView addSubview:contentLbl];
    }
}
- (void)hideNoContentView
{
    [self.noContentView removeFromSuperview];
    self.noContentView = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
